# -*- coding: utf-8 -*-


import getdata
from reportlab.pdfgen.canvas import Canvas
from reportlab.lib.pagesizes import letter
from reportlab.lib.units import cm, mm, inch, pica
from PIL import Image
import glob, os
import string, os, sys, imp, time
from reportlab.lib.logger import warnOnce
from types import *
from reportlab.lib.colors import  black, red ,green


# pdf dosyası başlangıç
pdf = Canvas("test.pdf")

import reportlab.rl_config
reportlab.rl_config.warnOnMissingFontGlyphs = 0
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont

pdfmetrics.registerFont(TTFont('Vera', 'Vera.ttf'))
pdf.setFont("Vera", 10)
pdf.drawString(10, 10, "NEN (c) 2008")

pdf.setStrokeColorRGB(1, 0, 0)
pdf.drawImage("splash.jpg",cm*1 ,cm * 27.5 , width=6*cm,height=2*cm)



# Database ile Türkçe karakter
getdata.cursor.execute("SET NAMES 'latin5'")
getdata.cursor.execute("SET collation_connection = 'latin5_turkish_ci'")
fisno="144"
# sipariş fiş tipi ve fiş no ya göre sip_id tespit ediliyor. Sip tarihi ve cari kod belirleniyor
getdata.cursor.execute( "select id,tarih,cari_kod from sipfis where fis_tipi=2 and fis_no=%s ",(fisno))
data = getdata.cursor.fetchall()

tarih=data[0][1]
tarih=str(tarih.day)+"/"+str(tarih.month)+"/"+str(tarih.year)
print tarih

firma= data[0][2]
#firma=firma.decode("iso-8859-1")
print data[0][2]
print firma
getdata.cursor.execute( "select adi,ilgili from cari where cari_no= %s ",(firma))
fdata=getdata.cursor.fetchall()
print fdata
adi= fdata[0][0]
firma=adi.decode("iso-8859-9")


# Sip_id 'ye göre siparişin satır bilgileri alınıyor.
getdata.cursor.execute( "select  stok_kod,var1,var2,miktar, fiyat,doviz from sipfisd where  sip_id=%s order by stok_kod,var1,var2",(data[0][0]) )
satir= getdata.cursor.fetchall()
print len(satir)
print satir #[0][1]

# Satır bilgileri işleniyor. String haline getiriliyor.


pdf.drawString(20,720,"Açıklama")
pdf.drawString(340,720,"Varyant")

pdf.drawString(395,720,"Miktar")
pdf.drawString(450,720,"Birim Fiyat")

pdf.setStrokeColor(black)
pdf.rect(18,550,522,166,fill=0)
pdf.rect(18,716,522,18,fill=0)    

for n in range(0,len(satir)):
    getdata.cursor.execute( "select stok_adi from stok where stok_no= %s ",(satir[n][0]))
    kdata=getdata.cursor.fetchall()
    a= kdata[0][0]
    a=a.decode("iso-8859-9")
    b= satir[n][1]
    c= satir[n][2]
    d= str(int(satir[n][3]))
    e= str(round(satir[n][4],4))
    f= satir[n][5]
    pdf.drawString(20,700-(n*15),a)
    pdf.drawString(350,700-(n*15),b+c)
#   pdf.drawString(350,700-(n*15),c)
    pdf.drawString(400,700-(n*15),d)
    pdf.drawString(450,700-(n*15),e)
    pdf.drawString(500,700-(n*15),f)
    pdf.rect(18,695-(n*15),522,15,fill=0)
    
pdf.setFont("Vera", 12)
pdf.drawString(20,750,firma)
pdf.drawString(450,750,tarih)
pdf.drawString(450,765,"Fiş No : "+fisno)
pdf.setFont("Vera", 28)
pdf.drawString(230,800,"SATINALMA SÖZLEŞMESİ ")
        


pdf.showPage()
pdf.save()

#getdata.cursor.close()

os.startfile("test.pdf")
#win32api.ShellExecute (0, "print", "test.pdf", None, ".", 0)
